//-----------------------------------------------------------------------------
// File: Configurator.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using Cerebrum.Desktop.Configuration;

namespace ObjectiveDraw.Platform.Configuration
{
	/// <summary>
	/// Summary description for CommandTranslator.
	/// </summary>
	public class Configurator: Cerebrum.Windows.Forms.Reflection.UiServiceDescriptor
	{
		public Configurator()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public ObjectiveDraw.Platform.Application DrawerApplication
		{
			get
			{
				return this.DomainContext.Properties[ObjectiveDraw.Platform.Application.ApplicationProperty] as ObjectiveDraw.Platform.Application;
			}
		}

		public override bool CommandActivated(Cerebrum.IConnector message, object sender, object argument)
		{
			System.Windows.Forms.DialogResult dr = System.Windows.Forms.DialogResult.OK;

			string messageName = ((Cerebrum.Reflection.MessageDescriptor)message.Component).Name;

			if(argument is ConfigureInstanceEventArgs)
			{
				ConfigureInstanceEventArgs ce = argument as ConfigureInstanceEventArgs;
				IDrawerComponent component = (IDrawerComponent)ce.Instance;
				IConfigurationElement element = ce.Description as IConfigurationElement;
				Cerebrum.Integrator.DomainContext componentDomainContext = component.DomainContext;

				if(element==null)
				{
					switch(messageName)
					{
						case ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate:
						{
							if(component is ObjectiveDraw.Platform.Application)
							{
								element = ObjectiveDraw.Platform.Configuration.Tools.BuildApplicationTree(component, componentDomainContext);
							}
							if(component is ObjectiveDraw.Platform.DrawerDocument)
							{
								element = ObjectiveDraw.Platform.Configuration.Tools.BuildWorkspaceTree(component, componentDomainContext);
							}
							if(component is ObjectiveDraw.Platform.GraphChartDevice)
							{
								element = ObjectiveDraw.Platform.Configuration.Tools.BuildWindowTree(component, componentDomainContext, new System.Collections.Hashtable());
							}
							if(component is ObjectiveDraw.Platform.DrawerChartBase)
							{
								Cerebrum.Desktop.Configuration.ConfigurationElement root = new Cerebrum.Desktop.Configuration.ConfigurationElement();
								root.Caption = "Chart";

								root.Elements.Add(Tools.BuildChartTree(component, componentDomainContext, null));
								System.Collections.Hashtable SingleVariableHash = new System.Collections.Hashtable();
								SingleVariableHash[Tools.KnownConfiguratorContext.Parent] = Tools.KnownConfiguratorContext.Chart;									
								root.Elements.Add(Tools.BuildProportionsTree(component, componentDomainContext, SingleVariableHash));
								element = root;
							}
							break;
						}
						case ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationCreate:
						{
							break;
						}
					}
				}

				if(component!=null && element!=null)
				{
					/*--component.Serialize(new SerializationContext(component.Master, SerializationContextMotive.Accepting), Cerebrum.SerializeDirection.Save);

					ObjectiveDraw.Platform.Application application = ObjectiveDraw.Platform.Application.FromComponent(this);
					ObjectiveDraw.Platform.Workspace wks = application.ActiveWorkspace;
					if(wks!=null) wks.WorkspaceDatabase.AcceptChanges();--*/
					ConfiguratorDialog cfg = new ConfiguratorDialog();
					cfg.Accepting += new EventHandler(Configurator_Accepting);
					cfg.Rejecting += new EventHandler(Configurator_Rejecting);
					switch(messageName)
					{
						case ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationCreate:
						{
							cfg.AllowApply = false;
							break;
						}
					}
					Cerebrum.Runtime.SurrogateComposite surrogate = new Cerebrum.Runtime.SurrogateComposite(componentDomainContext.Workspace);
					((Cerebrum.IComponent)cfg).SetConnector(Cerebrum.SerializeDirection.None, surrogate);
					Cerebrum.Desktop.Configuration.ComponentFactory bldr = new Cerebrum.Desktop.Configuration.ComponentFactory();
					((Cerebrum.IComponent)bldr).SetConnector(Cerebrum.SerializeDirection.None, surrogate);
					cfg.EditorFactory = bldr;
					cfg.ExecuteInvoke(new ConfigureInstanceEventArgs(/*--e.Command,--*/ component, element));
					/*--Cerebrum.Integrator.InvokeCommandEventArgs icea = new Cerebrum.Integrator.InvokeCommandEventArgs(m_DomainContext.PrimaryWindow.GetCommand("show"));
					cfg.Execute(this, icea);--*/
					dr = cfg.ShowDialog();
					cfg.Accepting -= new EventHandler(Configurator_Accepting);
					cfg.Rejecting -= new EventHandler(Configurator_Rejecting);
					//--e.Result = icea.Result;
				}
				//--e.Cancel = true;
			}
			else
			{
				if(messageName!=null)
				{
					if(messageName=="TradingPlatform.Application.Options")
					{
						//--ObjectiveDraw.Platform.Application application = ObjectiveDraw.Platform.Application.FromComponent(this);
						//--this.Execute(this, new ConfigureInstanceEventArgs(application.SystemContext.PrimaryWindow.GetCommand(ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate), application, null));
						//--e.Cancel = true;

					}

					if(messageName==ObjectiveDraw.Platform.KnownDrawerCommands.DatabaseOptions)
					{
						using(Cerebrum.IComposite connector = this.m_Connector.Workspace.AttachConnector(Cerebrum.Management.Utilites.ResolveHandle(this.DomainContext, "Messages", "Name", ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate)))
						{
							return this.CommandActivated(connector, sender, new ConfigureInstanceEventArgs(DrawerApplication.ActiveDocument, null));
						}
					}
				}
			}
			bool r = base.CommandActivated (message, sender, argument);
			return dr == System.Windows.Forms.DialogResult.OK;
		}

		private void Configurator_Accepting(object sender, EventArgs e)
		{
			/*--Configuration.ConfigureInstanceEventArgs ce = e as Configuration.ConfigureInstanceEventArgs;
			if(ce!=null && ce.Instance!=null)
			{
				ce.Instance.Serialize(new SerializationContext(ce.Instance.Workspace, SerializationContextMotive.Accepting), Cerebrum.SerializeDirection.Save);
			}
			ObjectiveDraw.Platform.Application application = ObjectiveDraw.Platform.Application.FromComponent(this);
			ObjectiveDraw.Platform.Workspace wks = application.ActiveWorkspace;
			if(wks!=null) wks.WorkspaceDatabase.AcceptChanges();--*/
		}

		private void Configurator_Rejecting(object sender, EventArgs e)
		{
			/*--ObjectiveDraw.Platform.Application application = ObjectiveDraw.Platform.Application.FromComponent(this);
			ObjectiveDraw.Platform.Workspace wks = application.ActiveWorkspace;
			if(wks!=null) wks.WorkspaceDatabase.RejectChanges();
			Configuration.ConfigureInstanceEventArgs ce = e as Configuration.ConfigureInstanceEventArgs;
			if(ce!=null && ce.Instance!=null)
			{
				ce.Instance.Serialize(new SerializationContext(ce.Instance.Workspace, SerializationContextMotive.Rejecting), Cerebrum.SerializeDirection.Take);
			}--*/
		}
	}
}
